package com.hotel.config.rabbitmq.customer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotel.config.websocket.WebSocketProcess;
import com.hotel.customersystem.enums.CusOrderStatus;
import com.hotel.customersystem.pojo.CustomerOrder;
import com.hotel.customersystem.service.CustomerOrderService;
import com.hotel.hotelsystem.pojo.RoomInfo;
import com.hotel.hotelsystem.service.RoomInfoService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Map;

import static com.hotel.config.rabbitmq.config.OrderDelayConfig.*;

@Slf4j
@Component
public class OrderDelayConsumer {
    @Resource
    private CustomerOrderService customerOrderService;
    @Resource
    private RoomInfoService roomInfoService;

    @Resource
    private WebSocketProcess webSocketProcess;


    //监听延迟队列
    @RabbitListener(queues = ORDER_DELAY_QUEUE_NAME)
    public void receive(Map<String,Object> map, Message message, Channel channel){
        String orderNo = (String) map.get("orderNo");
        Integer userId = (Integer) map.get("userId");
        log.info("订单号:{},用户账号:{}",orderNo,userId);
        Integer result = customerOrderService.orderDelayChangeStatus(orderNo);
        //修改订单状态 已支付


//        if (result!=0){
//            try {
//                webSocketProcess.sendMessage(String.valueOf(userId),"您的订单:"+orderNo+"已过期");
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
        log.info("现在的时间是:{},延迟队列收到的消息:{},操作结果是:{}", LocalDateTime.now(),orderNo,result);
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
